#include <QtGui/QApplication>

#include <QFile>
#include <QProcess>
#include <QSettings>
#include <QDir>
#include <QStringList>
#include <QTimer>
#include <QFileInfo>
#include <QTextCodec>

#include <windows.h>

struct tagFileInfo
{
    QString m_name;
    int     m_type;
    char*   m_data;
    int     m_size;
};

QString applicationPath;

bool update()
{
    QString path=applicationPath+"/update.db";

    QFile file(path);
    QList<tagFileInfo*>     list;
    if(file.open(QFile::ReadWrite))
    {
        QByteArray array=file.readAll();
        file.close();
        file.remove();
        if(array.size()<=4)
        {
            goto LOOP;
        }
        uint sum=0;
        for(int i=0;i<array.size()-4;i++)
        {
            sum+=(array.at(i)&0xff);
        }
        QByteArray temp=array.right(4);
        if(sum!=(uint)(((temp.at(0)&0xff)<<24)+
                 ((temp.at(1)&0xff)<<16)+
                 ((temp.at(2)&0xff)<<8)+
                 (temp.at(3)&0xff)))
        {
            goto LOOP;
        }
        temp=array.left(array.size()-4);
        char* data=temp.data();
        int pt=0;
        int size=temp.size();
        while(pt<size)
        {
            tagFileInfo *info=new tagFileInfo;
            list.append(info);
            info->m_name=QString(data+pt);
            pt+=(strlen(data+pt));
            if(pt>=size)
            {
                goto LOOP;
            }
            info->m_type=((data[pt]&0xff)<<24)
                    +((data[pt+1]&0xff)<<16)
                    +((data[pt+2]&0xff)<<8)
                    +((data[pt+3]&0xff));
            pt+=4;
            if(pt>=size)
            {
                goto LOOP;
            }
            info->m_size=((data[pt]&0xff)<<24)
                    +((data[pt+1]&0xff)<<16)
                    +((data[pt+2]&0xff)<<8)
                    +((data[pt+3]&0xff));
            pt+=4;
            if(pt>size)
            {
                goto LOOP;
            }
            info->m_data=data+pt;
            pt+=info->m_size;
            if(pt>size)
            {
                goto LOOP;
            }
        }

        foreach(tagFileInfo *info,list)
        {
            if(info->m_type==1)
            {
                QString str=applicationPath+"/"+info->m_name;
                str.replace("\\","/");
                int index=str.lastIndexOf("/");
                QDir dir;
                dir.mkpath(str.left(index));
                QFile f(str);
                if(f.open(QFile::WriteOnly))
                {
                    f.resize(0);
                    f.write(info->m_data,info->m_size);
                    f.close();
                }
            }
            else if(info->m_type==2)
            {
                QString str=applicationPath+"/plugins/"+info->m_name;
                str.replace("\\","/");
                int index=str.lastIndexOf("/");
                QDir dir;
                dir.mkpath(str.left(index));
                QFile f(str);
                if(f.open(QFile::WriteOnly))
                {
                    f.resize(0);
                    f.write(info->m_data,info->m_size);
                    f.close();
                }
            }
            else
            {
                QSettings s(applicationPath+"/config.ini",QSettings::IniFormat);
                QString proPath=s.value("project/path").toString();
                if(proPath=="")
                {
                    proPath=applicationPath+"/project";
                }
                QString str=proPath+"/"+info->m_name;
                str.replace("\\","/");
                int index=str.lastIndexOf("/");
                QDir dir;
                dir.mkpath(str.left(index));
                QFile f(str);
                if(f.open(QFile::ReadWrite))
                {
                    f.resize(0);
                    f.write(info->m_data,info->m_size);
                    f.close();
                }
            }
        }
        return true;
    }

    LOOP:
    qDeleteAll(list);
    return false;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTextCodec *gbk = QTextCodec::codecForName("GBK");
    QTextCodec::setCodecForTr(gbk);
    QTextCodec::setCodecForLocale(gbk);
    QTextCodec::setCodecForCStrings(gbk);

    applicationPath=a.applicationDirPath();
    bool ret=update();

    QStringList list;
    list<<QString("update=%1").arg(ret?"true":"false");

    QProcess::startDetached(applicationPath+"/QDeviceSoft.exe",list);
    return 0;
}
